package com.zm.devmag.base.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
//import com.zm.devmag.base.vo.ResponseMsg;
import com.zm.devmag.util.IOUtils;
import com.zm.devmag.util.PicUtils;

//@RestController
/*public class ImgController {
	
	@ResponseBody
	@RequestMapping("/imgUpload")
	public Object imgUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, HttpServletResponse response )throws IOException {
		   if(file.getSize() !=0){
	            String fileName = file.getOriginalFilename();
	            String reg = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$";
	            Pattern pattern = Pattern.compile(reg);
	            Matcher matcher = pattern.matcher(fileName.toLowerCase());
	            if(!matcher.find()){
	                return new ResponseMsg("error","图片格式不正确，请上传 jpeg,jpg,gif,bmp,png格式图片");
	            };

	            if(file.getSize() > 1024*1024*2){
	            	return new ResponseMsg("error", "图片最大2M！");
	            }
	            String uuidname = UUID.randomUUID().toString();
				String hash = Integer.toHexString(uuidname.hashCode());
				String upload = request.getServletContext().getRealPath("/upload");
				String webPath = "/upload";
				for(char c : hash.toCharArray()){
					upload+="/"+c;
					webPath +="/"+c;
				}
				String fileType = fileName.substring(fileName.lastIndexOf("."));
				File uploadFile = new File(upload);
				if(!uploadFile.exists()){
					uploadFile.mkdirs();
				}
				upload +="/"+uuidname  + fileType;
				webPath += "/" + uuidname + fileType;
				InputStream in = file.getInputStream();
				OutputStream out = new FileOutputStream(upload);
	            IOUtils.In2Out(in,out);
	            IOUtils.close(in,out);
	        	//--生成缩略图
				PicUtils picu = new PicUtils(request.getServletContext().getRealPath(webPath));
				picu.resizeByHeight(140);
	            return new ResponseMsg("success", webPath);
	          }
	      
	        return new ResponseMsg("error", "请不要上传空文件！");
	}
	@RequestMapping("/imgDownload")
	public void downloadImg(String path,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		request.getRequestDispatcher(path).forward(request,response);
	}
	@ResponseBody
	@RequestMapping("/imgUploadBase64")
	public Object ImgUploadBase64(@RequestParam String Imgbase64 ,HttpServletRequest request, HttpServletResponse response )throws IOException {
	  
      
        if(null == Imgbase64 || "".equals(Imgbase64))  
        {  
        	return new ResponseMsg("error","图片格式不正确，请上传 jpeg,jpg,gif,bmp,png格式图片");
        }  
          
        String uuidname = UUID.randomUUID().toString();
		String hash = Integer.toHexString(uuidname.hashCode());
		String upload = request.getServletContext().getRealPath("/upload");
		String webPath = "/upload";
		for(char c : hash.toCharArray()){
			upload+="/"+c;
			webPath +="/"+c;
		}
		
		File uploadFile = new File(upload);
		if(!uploadFile.exists()){
			uploadFile.mkdirs();
		}
		upload +="/"+uuidname  + ".jpg";
		webPath += "/" + uuidname +  ".jpg";  
        try{  
            Base64 base64 = new Base64();  
            byte[] bytes = base64.decodeBase64(new String(Imgbase64).getBytes());  
              
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);  
            byte[] buffer = new byte[1024];  
            FileOutputStream out = new FileOutputStream(upload);  
            int bytesum = 0;  
            int byteread = 0;  
            while ((byteread = in.read(buffer)) != -1) {  
                bytesum += byteread;  
                out.write(buffer, 0, byteread); // 文件写操作  
                out.flush();  
            }  
            out.close();  
            PicUtils picu = new PicUtils(request.getServletContext().getRealPath(webPath));
			picu.resizeByHeight(140);
            return new ResponseMsg("success", webPath); 
            
        }catch (Exception e) {  
           
            System.out.println("攒文件势必");  
        }  
       
	    return new ResponseMsg("error", "请不要上传空文件！");
	}
}*/
